package com.shj.controller;

import com.shj.entity.SysPolicy;
import com.shj.service.SysPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/system/policy")
public class SysPolicyController {
    @Autowired
    private SysPolicyService sysPolicyService;

    @RequestMapping("/list")
    public String findAll(Model model) {
        List<SysPolicy> list = sysPolicyService.findAll();
        model.addAttribute("list", list);
        return "policy/policy_list";
    }

    @RequestMapping("/openAdd")
    public String openAdd(Model model) {
        return "policy/policy_add";
    }

    @RequestMapping("/add")
    public String add(Model model, SysPolicy sysPolicy) {
        sysPolicyService.add(sysPolicy);
        return findAll(model);
    }

    @RequestMapping("/unallow")
    public String openEdit(Model model, String year){
        SysPolicy sysPolicy = sysPolicyService.findByYear(year);
        sysPolicy.setStatus("0");
        sysPolicyService.edit(sysPolicy);
        return findAll(model);
    }

    @RequestMapping("/allow")
    public String edit(Model model, String year) {
        SysPolicy sysPolicy = sysPolicyService.findByYear(year);
        sysPolicy.setStatus("1");
        sysPolicyService.edit(sysPolicy);
        return findAll(model);
    }

    @RequestMapping("/delete")
    public String delete(Model model, String year){
        sysPolicyService.delete(year);
        return findAll(model);
    }
}
